package com.itany.cloud.controller;

import com.itany.cloud.entity.User;
import com.itany.cloud.service.UserService;
import com.itany.cloud.util.ResponseResult;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 石小俊
 */
@RestController
@RequestMapping("/users")
@Tag(name = "用户微服务接口", description = "主要用于实现用户微服务中的业务功能")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    @Operation(summary = "根据主键查询", description = "根据主键id查询对应的用户信息")
    @Parameter(name = "id", description = "主键id")
    public ResponseResult findById(@PathVariable Integer id) {
        User user = userService.findById(id);
        return ResponseResult.success(user);
    }

    @GetMapping
    @Operation(summary = "查询所有用户")
    public ResponseResult findAll() {
        List<User> users = userService.findAll();
        return ResponseResult.success(users);
    }

    @PostMapping
    @Operation(summary = "添加用户")
    @Parameters({
            @Parameter(name = "username", description = "用户名参数"),
            @Parameter(name = "password", description = "密码参数")
    })
    public ResponseResult addUser(User user) {
        userService.addUser(user);
        return ResponseResult.success();
    }

    @PutMapping
    @Operation(summary = "修改用户信息")
    public ResponseResult modifyUser(User user) {
        userService.modifyUser(user);
        return ResponseResult.success();
    }

    @DeleteMapping("/{id}")
    @Hidden
//    @Operation(summary = "删除用户信息")
//    @Parameter(name = "id",description = "主键id")
    public ResponseResult deleteUser(@PathVariable Integer id) {
        userService.deleteById(id);
        return ResponseResult.success();
    }

}
